package com.zzptc.shop.util;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyUtil {
    //读取配置文件
    public static Map<String, String> readXML() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        SAXBuilder saxBuilder = new SAXBuilder();
        String path = MyUtil.class.getResource("/").getPath() + "dbconfig.xml";
        Document doc = saxBuilder.build(path);
        Element rootelment = doc.getRootElement();
        System.out.println("根节点:" + rootelment.getName());

        List<Element> listall = rootelment.getChildren();
        // for(int i=0;i<listall.size();i++){
        //    System.out.println("子节点"+i+":"+listall.get(i).getName());
        // }
        Iterator iter = listall.iterator();//拿到迭代器
        while (iter.hasNext()) {//迭代器有几个方法  判断有没有元素hasNext() 有元素就next()
            Element e = (Element) iter.next();
            //System.out.println(e.getName()+"^^^^^^"+e.getText());
            //放入map
            map.put(e.getName(),e.getText());
        }

        return map;
    }

    public static void main(String[] args) {
        try {
            MyUtil.readXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
